package com.alm.token.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.alm.token.ConStant;
import com.alm.token.R;
import com.alm.token.base.BaseActivity;
import com.alm.token.ui.data.BaseDialogBean;
import com.alm.token.ui.data.bean.BuyHMioAddData;
import com.alm.token.ui.data.bean.BuyHMioTransIndexData;
import com.alm.token.ui.data.bean.RegisterData;
import com.alm.token.ui.fragmentDialog.BuyHMioSubmitFragment;
import com.alm.token.ui.fragmentDialog.CandyDialogFragment;
import com.alm.token.ui.persenter.MyPreenter;
import com.alm.token.ui.view.BuyHMioView;
import com.alm.token.utils.ActionBarHelper;
import com.alm.token.utils.CommonUtil;
import com.alm.token.utils.EmptyUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import baibeiwallet.event.getUserWalletEvent;
import butterknife.BindView;
import butterknife.ButterKnife;

public class BuyHMioTransIndexActivity extends BaseActivity implements BuyHMioView<BuyHMioTransIndexData> {


    @BindView(R.id.tv_nowratio)
    TextView tvNowratio;
    @BindView(R.id.tv_nowsort)
    TextView tvNowsort;
    @BindView(R.id.tv_totalNum)
    TextView tvTotalNum;
    @BindView(R.id.tv_havenum)
    TextView tvHavenum;
    @BindView(R.id.tv_haveRatio)
    TextView tvHaveRatio;
    @BindView(R.id.tv_buycommit)
    TextView tvBuycommit;
    @BindView(R.id.tv_nextsort)
    TextView tvNextsort;
    @BindView(R.id.tv_nexttotalNum)
    TextView tvNexttotalNum;
    @BindView(R.id.tv_nextratio)
    TextView tvNextratio;

    List<BuyHMioTransIndexData.DataBean.WalletsBean> wallets = new ArrayList<>();
    BuyHMioTransIndexData.DataBean dataBean;

    @Override
    protected MyPreenter initPreenter() {
        return preenter = new MyPreenter(this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_buyhmiotransindex;
    }

    @Override
    protected void initVariables() {

    }

    @Override
    protected void initViews() {
        new ActionBarHelper(this).setTitle(getString(R.string.main_hint_conversion) + " " + getString(R.string.hmio_hint_title));
    }

    @Override
    protected void loadData() {
        preenter.getHMioTransIndexData();
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.tv_buycommit:

                if(EmptyUtils.isNotEmpty(wallets)){
                    showBuyHMioDialog();
                }
                break;

        }
    }


    @Override
    public void getData(BuyHMioTransIndexData buyHMioTransIndexData) {
        if (buyHMioTransIndexData.getStatus() == 0) {
            dataBean = buyHMioTransIndexData.getData();
            if (EmptyUtils.isNotEmpty(dataBean)) {
                //nowPrice : {"totalNum":"1900000","balanceNum":"1298600.0003","price":"0.2000","sort":1,"ratio":"68"}
                BuyHMioTransIndexData.DataBean.NowPriceBean nowPrice = dataBean.getNowPrice();
                //nextPrice : {"totalNum":"1809523.8095","balanceNum":"1809523.8095","price":"0.2100","sort":2,"ratio":"100"}
                BuyHMioTransIndexData.DataBean.NextPriceBean nextPrice = dataBean.getNextPrice();
                wallets = dataBean.getWallets();

                tvNowratio.setText("1 USDT = " + CommonUtil.saveToPoint4WithNo4(1 / dataBean.getPrice()) + " " + dataBean.getName());
                if (EmptyUtils.isNotEmpty(nowPrice)) {
                    tvNowsort.setText(" " + nowPrice.getSort());
                    tvTotalNum.setText(CommonUtil.saveToPoint4WithNo4(nowPrice.getTotalNum()));
                    tvHavenum.setText(CommonUtil.saveToPoint4WithNo4(nowPrice.getBalanceNum()));
                    tvHaveRatio.setText(CommonUtil.saveToPoint2WithNo(nowPrice.getTotalNum() != 0 ? nowPrice.getBalanceNum() * 100 / nowPrice.getTotalNum() : 0) + "%");
                }
                if (EmptyUtils.isNotEmpty(nextPrice)) {
                    tvNextsort.setText("" + nextPrice.getSort());
                    tvNexttotalNum.setText(CommonUtil.saveToPoint4WithNo4(nextPrice.getTotalNum()));
                    tvNextratio.setText("1:" + CommonUtil.saveToPoint4WithNo4(1 / nextPrice.getPrice()));
                }
                tvBuycommit.setOnClickListener(this);
            }

        } else {
            showToast(buyHMioTransIndexData.getMsg());
        }
    }

    @Override
    public void getBuyHMioAddOrder(BuyHMioAddData registerData) {
        showToast(registerData.getMsg());
        if (registerData.getStatus() == 0) {
            EventBus.getDefault().post(new getUserWalletEvent(null));
            loadData();
        }
    }

    @Override
    public void jumpToLoginAct(int staue) {
        if (staue == 3) {
//            showToast(getString(R.string.common_relogin));
//            Intent intent = new Intent();
//            intent.setAction(ConStant.EXIT_APP);
//            sendBroadcast(intent);
//            startActivity(BlockWalletLoginActivity.class);
//            finish();
        } else if (staue == 4) {
//            showToast(getString(R.string.paypasw_hint_setpaypsw));
//            Intent intent = new Intent(this,PayPwdActivity.class);
//            intent.putExtra("PayPwdActivityType",PayPwdActivity.PAYPSW_SET);
//            startActivity(intent);
//            finish();
            showToSetPaypswDialog();
        } else if (staue == 666) {
            showToUpdata();
        }
    }

    @Override
    public void onError(String error) {

        showToast(getString(R.string.common_loaderrritry));
    }

    @Override
    public void ShowProgress() {

        showWaitDialog();
    }

    @Override
    public void hideProgress() {

        hideWaitDialog();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


    @Override
    protected void onPause() {
        super.onPause();
//        if (EmptyUtils.isNotEmpty(timer)) {
//            timer.cancel();
//        }
    }


    //     EventBus.getDefault().post(new getUserWalletEvent(null));
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onUserWalletEvent1(getUserWalletEvent userWalletEvent) {
        loadData();
    }

    @Override
    public void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    /***  弹出提示公告  ***/
    private void showBuyHMioDialog() {
        BuyHMioSubmitFragment dialogFragment = new BuyHMioSubmitFragment();
        dialogFragment.setDialogBean(new BaseDialogBean(wallets,dataBean,this));

        dialogFragment.setListener(new BuyHMioSubmitFragment.ClickListener() {
            @Override
            public void Listener(String name, int potion) {

            }
        });

        dialogFragment.show(getSupportFragmentManager(), "BuyHMioTransIndexActivity");
        getFragmentManager().executePendingTransactions();
    }

}
